set -e

cat > /etc/fstab << "EOF"
# Begin /etc/fstab

# 文件系统     挂载点       类型     选项                转储  检查
#                                                              顺序

/dev/sdb2     /            ext4    defaults            1     1

# End /etc/fstab
EOF

# 编译内核
pushd /sources
    # tar -xvf linux-6.13.4.tar.xz
    cd linux-6.13.4
    # make mrproper
    make menuconfig
    make
    make modules_install
    cp -iv arch/x86/boot/bzImage /boot/vmlinuz-6.13.4-lfs-12.3-systemd
    cp -iv System.map /boot/System.map-6.13.4
    cp -iv .config /boot/config-6.13.4
    cp -r Documentation -T /usr/share/doc/linux-6.13.4
    
popd

chown -R 0:0 /sources/linux-6.13.4
install -v -m755 -d /etc/modprobe.d
cat > /etc/modprobe.d/usb.conf << "EOF"
# Begin /etc/modprobe.d/usb.conf

install ohci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i ohci_hcd ; true
install uhci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i uhci_hcd ; true

# End /etc/modprobe.d/usb.conf
EOF

# 使用 GRUB 设定引导过程
grub-install /dev/sdb

cat > /boot/grub/grub.cfg << "EOF"
# Begin /boot/grub/grub.cfg
set default=0
set timeout=5

insmod part_gpt
insmod ext2
# set root=(hd1,2)
search --set=root --fs-uuid c5902575-db53-4e4e-abce-131e1c2e780d
set gfxpayload=1024x768x32

menuentry "GNU/Linux, Linux 6.13.4-lfs-12.3-systemd" {
        # linux   /boot/vmlinuz-6.13.4-lfs-12.3-systemd root=/dev/sdb2 ro
        linux   /boot/vmlinuz-6.13.4-lfs-12.3-systemd root=PARTUUID=0ded3c88-1bb0-4989-a7d4-498d7230b68b ro
}
EOF